package com.ma.workorder;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.ma.R;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import bean.LoginBean;
import database.table.Dbb;
import database.table.Gdb;
import httpUtil.Api;
import httpUtil.Http;
import utils.ConstantUtil;

/**
 * Created by loma on 2017/10/11.
 */

public class SuperviseDialog extends AlertDialog {
    LoginBean loginBean;
    Gdb gdb;

    public SuperviseDialog(Context context, LoginBean loginBean, Gdb gdb) {
        super(context);
        this.loginBean = loginBean;
        this.gdb = gdb;

    }

    public SuperviseDialog(Context context) {
        super(context);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_supervise);
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.TOP;
        window.setAttributes(lp);

        initView();

    }

    private void initView() {


        ImageView close = findViewById(R.id.iv_close);
        close.setOnClickListener(view1 -> {
            dismiss();
        });
        EditText text = findViewById(R.id.editText);
        Button button = findViewById(R.id.ok_bt);
        button.setOnClickListener(view1 -> {
            button.setEnabled(false);
            Map<String, Object> map = new HashMap<>();
            map.put("DBB_GDBWJ", gdb.getGDB_ZJ());
            map.put("DBB_DBR", loginBean.getRealName());
            map.put("DBB_DBRWJ", loginBean.getID());
            map.put("DBB_SFCQ", gdb.getGDB_SFCQ());
            map.put("DBB_DBLR", text.getText().toString());

            Http.post(Api.AddDBB, map)
                    .subscribe(res -> {
                        button.setEnabled(true);
                        if (res.getCode() == 200)  {
                            EventBus.getDefault().post(1);
                            dismiss();}
                        else Toast.makeText(getContext(), "提交失败", Toast.LENGTH_SHORT).show();

                    }, throwable -> {
                        button.setEnabled(true);
                        Toast.makeText(getContext(), "提交失败", Toast.LENGTH_SHORT).show();
                    });

        });


    }
}
